package com.hiwhitley.himei.module.video;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hiwhitley.himei.R;
import com.hiwhitley.himei.app.base.BaseActivity;
import com.hiwhitley.himei.entity.VideoDetail;
import com.hiwhitley.himei.widget.glide.GlideImageView;
import com.shuyu.gsyvideoplayer.GSYPreViewManager;
import com.shuyu.gsyvideoplayer.GSYVideoPlayer;
import com.shuyu.gsyvideoplayer.listener.LockClickListener;
import com.shuyu.gsyvideoplayer.utils.OrientationUtils;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

import butterknife.BindView;
import butterknife.OnClick;

import static com.hiwhitley.himei.module.meitu.PhotoActivity.TRANSIT_PIC;

/**
 * Created by hiwhitley on 2016/9/24.
 */

public class VideoDetailActivity extends BaseActivity {

    public static final String TRANSIT_VIDEO = "video";
    private static final String TAG = "VideoDetailActivity";
    public final static String VIDEO_DETAIL = "video_detail";
    @BindView(R.id.video_toolbar)
    Toolbar mVideoToolbar;
    @BindView(R.id.video_detail_ivmo)
    GlideImageView mVideoDetailIvmo;
    @BindView(R.id.video_detail_title)
    TextView mVideoDetailTitle;
    @BindView(R.id.video_detail_time)
    TextView mVideoDetailTime;
    @BindView(R.id.video_detail_desc)
    TextView mVideoDetailDesc;
    @BindView(R.id.video_detail_iv_fav)
    ImageView mVideoDetailIvFav;
    @BindView(R.id.video_detail_tv_fav)
    TextView mVideoDetailTvFav;
    @BindView(R.id.video_detail_iv_share)
    ImageView mVideoDetailIvShare;
    @BindView(R.id.video_detail_tv_share)
    TextView mVideoDetailTvShare;
    @BindView(R.id.video_detail_iv_reply)
    ImageView mVideoDetailIvReply;
    @BindView(R.id.video_detail_tv_reply)
    TextView mVideoDetailTvReply;
    @BindView(R.id.root_view)
    LinearLayout mRootLinearLayout;
    @BindView(R.id.standard_video_player)
    StandardGSYVideoPlayer mStandardGSYVideoPlayer;

    private VideoDetail mVideoDetail;

    private boolean isPlay;
    private boolean isPause;
    private OrientationUtils orientationUtils;

    public static Intent newIntent(Context context, VideoDetail detail) {
        Intent intent = new Intent(context, VideoDetailActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(VIDEO_DETAIL, detail);
        intent.putExtras(bundle);
        return intent;
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_video_detail;
    }

    @Override
    protected void init() {

        ViewCompat.setTransitionName(mStandardGSYVideoPlayer, TRANSIT_VIDEO);

        mVideoDetail = (VideoDetail) getIntent().getSerializableExtra(VIDEO_DETAIL);
        Log.d(TAG, "init" + mVideoDetail.toString());

        setSupportActionBar(mVideoToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        mVideoDetailIvmo.loadNetImage(mVideoDetail.getBlurred(), R.drawable.bg_holder_brown);

//      初始化界面
        mVideoDetailTitle.setText(mVideoDetail.getTitle());
        mVideoDetailTime.setText(mVideoDetail.getTime());
        mVideoDetailDesc.setText(mVideoDetail.getDesc());
//        mVideoDetailTvFav.setText(String.valueOf(mVideoDetail.getCollect()));
//        mVideoDetailTvReply.setText(String.valueOf(mVideoDetail.getReply()));
//        mVideoDetailTvShare.setText(String.valueOf(mVideoDetail.getShare()));
    }

    @Override
    protected void setListeners() {

        mStandardGSYVideoPlayer.setUp(mVideoDetail.getVideo(), true, null, mVideoDetail.getTitle());

        //增加封面
        GlideImageView imageView = new GlideImageView(this);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.loadNetImage(mVideoDetail.getFeed(), R.drawable.bg_holder_brown);
        mStandardGSYVideoPlayer.setThumbImageView(imageView);

        resolveNormalVideoUI();

        //外部辅助的旋转，帮助全屏
        orientationUtils = new OrientationUtils(this, mStandardGSYVideoPlayer);
        //初始化不打开外部的旋转
        orientationUtils.setEnable(false);

        mStandardGSYVideoPlayer.setIsTouchWiget(true);
        //关闭自动旋转
        mStandardGSYVideoPlayer.setRotateViewAuto(false);
        mStandardGSYVideoPlayer.setLockLand(false);
        mStandardGSYVideoPlayer.setShowFullAnimation(false);
        mStandardGSYVideoPlayer.setNeedLockFull(true);
        mStandardGSYVideoPlayer.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //直接横屏
                orientationUtils.resolveByClick();

                //第一个true是否需要隐藏actionbar，第二个true是否需要隐藏statusbar
                mStandardGSYVideoPlayer.startWindowFullscreen(VideoDetailActivity.this, true, true);
            }
        });

        mStandardGSYVideoPlayer.setStandardVideoAllCallBack(new SampleListener() {
            @Override
            public void onPrepared(String url, Object... objects) {
                super.onPrepared(url, objects);
                //开始播放了才能旋转和全屏
                orientationUtils.setEnable(true);
                isPlay = true;
            }

            @Override
            public void onAutoComplete(String url, Object... objects) {
                super.onAutoComplete(url, objects);
            }

            @Override
            public void onClickStartError(String url, Object... objects) {
                super.onClickStartError(url, objects);
            }

            @Override
            public void onQuitFullscreen(String url, Object... objects) {
                super.onQuitFullscreen(url, objects);
                if (orientationUtils != null) {
                    orientationUtils.backToProtVideo();
                }
            }
        });

        mStandardGSYVideoPlayer.setLockClickListener(new LockClickListener() {
            @Override
            public void onClick(View view, boolean lock) {
                if (orientationUtils != null) {
                    //配合下方的onConfigurationChanged
                    orientationUtils.setEnable(!lock);
                }
            }
        });

    }

    @OnClick({R.id.video_detail_iv_fav, R.id.video_detail_iv_share, R.id.video_detail_iv_reply})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.video_detail_iv_fav:
                break;
            case R.id.video_detail_iv_share:
                break;
            case R.id.video_detail_iv_reply:
                break;
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        isPause = true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        isPause = false;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        GSYVideoPlayer.releaseAllVideos();
        GSYPreViewManager.instance().releaseMediaPlayer();
        if (orientationUtils != null)
            orientationUtils.releaseListener();
    }

    @Override
    public void onBackPressed() {

        if (orientationUtils != null) {
            orientationUtils.backToProtVideo();
        }

        if (StandardGSYVideoPlayer.backFromWindowFull(this)) {
            return;
        }
        super.onBackPressed();
    }


    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        //如果旋转了就全屏
        if (isPlay && !isPause) {
            if (newConfig.orientation == ActivityInfo.SCREEN_ORIENTATION_USER) {
                if (!mStandardGSYVideoPlayer.isIfCurrentIsFullscreen()) {
                    mStandardGSYVideoPlayer.startWindowFullscreen(VideoDetailActivity.this, true, true);
                }
            } else {
                //新版本isIfCurrentIsFullscreen的标志位内部提前设置了，所以不会和手动点击冲突
                if (mStandardGSYVideoPlayer.isIfCurrentIsFullscreen()) {
                    StandardGSYVideoPlayer.backFromWindowFull(this);
                }
                if (orientationUtils != null) {
                    orientationUtils.setEnable(true);
                }
            }
        }
    }
    private void resolveNormalVideoUI() {
        //增加title
        mStandardGSYVideoPlayer.getTitleTextView().setVisibility(View.GONE);
        mStandardGSYVideoPlayer.getBackButton().setVisibility(View.GONE);
    }

}
